package com.china.school.resource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.support.ServletContextResource;

import javax.servlet.ServletContext;
import java.io.IOException;

@SpringBootApplication
public class ServletContextResourceTest {
    public static void main(String[] args) throws IOException {
        ConfigurableApplicationContext ctx = SpringApplication.run(ServletContextResourceTest.class);
        ServletContextAwareTest servletContextAwareTest = ctx.getBean(ServletContextAwareTest.class);
        ServletContextResource servletContextResource = new ServletContextResource(servletContextAwareTest.getServletContext()
        ,"aaa");
        System.out.println("获取项目路径 " + servletContextResource.getFile().getAbsolutePath());
    }

     @Component
     public static class  ServletContextAwareTest implements ServletContextAware {
        private ServletContext servletContext;

         @Override
         public void setServletContext(ServletContext servletContext) {
             this.servletContext = servletContext;
         }

         public ServletContext getServletContext() {
             return servletContext;
         }
     }
}
